package com.pdemo.core.genericdao;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@MappedSuperclass
public class BaseEntity implements IEntity<Long> {
	private static final long serialVersionUID = 5510109329475335461L;


	@Id
	@Column(name="id")
	@GeneratedValue(generator="sequence")
	@SequenceGenerator(name="sequence",sequenceName="ID_SEQ")
	private Long id;
	
	@Column(name="CREATED_AT")
	@Temporal(TemporalType.TIMESTAMP)
	private Date createdAt;
	
	@Column(name="UPDATED_AT")
	@Temporal(TemporalType.TIMESTAMP)
	private Date updateAt;



	
	public Date getCreatedAt() {
		return createdAt;
	}


	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}


	public Date getUpdateAt() {
		return updateAt;
	}


	public void setUpdateAt(Date updateAt) {
		this.updateAt = updateAt;
	}


	public Long getId() {
		return id;
	}

	
	public void setId(Long id) {
		this.id = id;
	}
	
	@PrePersist
	protected void onCreate() {
		Date d = new Date();
		this.setCreatedAt(d);
		this.setUpdateAt(d);
	}

	@PreUpdate
	protected void onUpdate() {
		this.setUpdateAt(new Date());
	}

}
